/*
 * SpaceBattleInterface.cpp
 *
 *  Created on: Oct 23, 2011
 *      Author: gonzalo
 */

#include "SpaceBattleInterface.h"
#include "../persistencia/Persistor.h"
#include "../common/Translator/Translator.h"
#include "../common/Constantes.h"
#include "../common/DTOImage.h"
#include <stdio.h>
#include <dirent.h>


SpaceBattleInterface::SpaceBattleInterface(std::string scenarioName,bool) {
	this->scenarioName = scenarioName + ".xml";
	esServer =true;
	shipStatus = NULL;
	readXML();
}

SpaceBattleInterface::SpaceBattleInterface(std::string owner){
	this->owner = owner;
	esServer = false;
	shipStatus = NULL;
}
SpaceBattleInterface::~SpaceBattleInterface() {
	if (shipStatus != NULL)
		delete shipStatus;
}

void SpaceBattleInterface::setShipStatus(ShipStatus* shipStatus){
	this->shipStatus = shipStatus;
}

ShipStatus* SpaceBattleInterface::getShip(){
	return shipStatus;
}

void SpaceBattleInterface::readXML(){
	int error = Persistor::getElements(this->scenarioName,this->listAsteroides);
	Persistor::getDimensions(this->scenarioName,&this->width,&this->height);

	if(error != PERSISTER_SUCCESS){
		Logger::debug("SpaceBattleInterface","readXML","Error en la lectura del XML");
	}
}

std::list<Asteroide*> SpaceBattleInterface::duplicateAsteroids()
{
    std::list<Asteroide*> retorno;
    std::list<Asteroide*>::iterator it;
    for(it = listAsteroides.begin();it != listAsteroides.end();++it){
        retorno.push_back(new Asteroide((*it)->getDTO()));
    }
    return retorno;
}

std::list<Asteroide*> SpaceBattleInterface::getAsteroides(){
	if (esServer) {
		return  duplicateAsteroids();
	}

	return Translator::translateAsteroideDTO(listDTOAsteroides);
}


std::list< std::pair<std::string,std::string>* >* SpaceBattleInterface::getShips(){
	return Persistor::getShipsModels();
}

std::string SpaceBattleInterface::getOwner(){
	return this->owner;
}

void SpaceBattleInterface::setAsteroides(std::list<DTOAsteroide*> listElements){
	this->listDTOAsteroides = listElements;
}

int SpaceBattleInterface::getHeight() const
{
    return height;
}

int SpaceBattleInterface::getWidth() const
{
    return width;
}

DTO* SpaceBattleInterface::getDTOMensaje(){
    DTO* ret = NULL;
    if (!this->dtoMensajes.empty()) {
		ret = this->dtoMensajes.front();
		this->dtoMensajes.pop_front();
    }
    return ret;
}

void SpaceBattleInterface::setDTOMensaje(DTO* dtoMensaje){
    this->dtoMensajes.push_back(dtoMensaje);
}

void SpaceBattleInterface::removeImages()
{

	DIR *dir;
	dir = opendir(TEMP_DIR);

	//Is it a folder to remove?
	if(!dir) return;

	struct dirent *d;
    char buf[256];
    while((d = readdir(dir))){
        //printf("%s\n", d->d_name);
        sprintf(buf, "%s/%s", TEMP_DIR, d->d_name);
        remove(buf);
        perror("remove");
    }

	rmdir(TEMP_DIR);
	perror("rmdir");

}

void SpaceBattleInterface::clearListDTOAsteroides(){
	//Borrar los asteroides si se pasaron por red
	DTOAsteroide* dtoAux = NULL;
	DTOImage* imageToDelete;
	while(!listDTOAsteroides.empty()){
		dtoAux = listDTOAsteroides.back();
		imageToDelete = new DTOImage(dtoAux->tipo->imagePath);

		delete imageToDelete;
		delete dtoAux;

		listDTOAsteroides.pop_back();
	}
}

void SpaceBattleInterface::clearListAsteroides(){
	//Borrar los asteroides
	Asteroide* asteroide = NULL;
	while(!listAsteroides.empty()){
		asteroide = listAsteroides.back();

		delete asteroide;

		listAsteroides.pop_back();
	}

}

void SpaceBattleInterface::setPlayScenarioDimensions(int width,int height){
	this->width = width;
	this->height = height;
}

void SpaceBattleInterface::resetClientInterface(){
    removeImages();

	clearListDTOAsteroides();
	clearListAsteroides();
	readXML();
	shipStatus = NULL;
}
